package com.epam.traning.taskmanager.db;

import org.json.JSONException;
import org.json.JSONObject;

import com.epam.traning.taskmanager.content.contract.TaskContract;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * The Class DBHelper.
 */
public class DBHelper extends SQLiteOpenHelper {

	/** The constant database version number. */
	private static final int DATABASE_VERSION = 1;

	/** The constant database file name. */
	private static final String DATABASE_NAME = "tasks.store.db";
	
	/** The Constant COOKIE_TABLE. */
	public static final String TASKS_TABLE = "TASKS";

	/** The Constant COOKIE_COLUMNS. */
	public static final String[] TASKS_COLUMNS = {TaskContract.TasksColumns.TASK_ID, TaskContract.TasksColumns.NAME};
	
	/** The Constant ID_COL_INDEX. */
	public static final int ID_COL_INDEX = 0;
	/** The Constant NAME_COL_INDEX. */
	public static final int NAME_COL_INDEX = 1;

	/** The Constant CREATE_COOKIE_TABLE_SQL. */
	public static final String CREATE_TASKS_TABLE_SQL = "CREATE TABLE "
			+ TASKS_TABLE + " (" + TASKS_COLUMNS[ID_COL_INDEX] + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ TASKS_COLUMNS[NAME_COL_INDEX] + " VARCHAR NOT NULL)";

	/** The Constant DROP_COOKIE_TABLE_SQL. */
	public static final String DROP_TASKS_TABLE_SQL = "DROP TABLE IF EXISTS "
			+ TASKS_TABLE;


	/**
	 * Instantiates a new dB helper.
	 * 
	 * @param context
	 *            the context
	 * @param name
	 *            the name
	 * @param factory
	 *            the factory
	 * @param version
	 *            the version
	 */
	public DBHelper(final Context context, final CursorFactory factory) {
		super(context, DATABASE_NAME, factory, DATABASE_VERSION);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public final void onCreate(final SQLiteDatabase db) {
		try {
			db.beginTransaction();
			db.execSQL(CREATE_TASKS_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public final void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
		try {
			db.beginTransaction();
			db.execSQL(DROP_TASKS_TABLE_SQL);
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		onCreate(db);
	}

	public Cursor getTasks() {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query(DBHelper.TASKS_TABLE, null,
				null, null, null, null,
				null);
		return cursor;
	}
	
	public long addTask(JSONObject jsonObject) {
		SQLiteDatabase db = getWritableDatabase();
		return db.insert(DBHelper.TASKS_TABLE, null, getContentValues(jsonObject));			
	}
	
	/**
	 * Gets the content values for PersistedCookie POJO.
	 * 
	 * @param pTask
	 *            cookie POJO
	 * @return the content values
	 */
	private ContentValues getContentValues(final JSONObject pTask) {
		ContentValues values = new ContentValues();
		try {
			values.put(DBHelper.TASKS_COLUMNS[DBHelper.NAME_COL_INDEX], pTask.getString("name"));
			values.put(DBHelper.TASKS_COLUMNS[DBHelper.ID_COL_INDEX], pTask.getLong("id"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return values;
	}
}
